/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.vertexnet.realestate.view.dialog;

import static com.vertexnet.realestate.constants.UIConstants.CUSTOMER;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.swing.JOptionPane;

import com.vertexnet.realestate.bo.PaymentUtility;
import com.vertexnet.realestate.bo.PaymentUtility.PaymentPlanUI;
import com.vertexnet.realestate.dao.HibernateUtil;
import com.vertexnet.realestate.interfaces.Controller;
import com.vertexnet.realestate.interfaces.DefaultDialog;
import com.vertexnet.realestate.interfaces.MainView;
import com.vertexnet.realestate.interfaces.View;
import com.vertexnet.realestate.model.RealEstateUtilityModel;
import com.vertexnet.realestate.model.tablemodel.SitePaymentTableModel;
import com.vertexnet.realestate.view.utility.AgentsComboBoxCellRenderer;
import com.vertexnet.realestate.view.utility.AgentsComboBoxesCellEditor;
import com.vertexnet.realestate.view.utility.CalenderCellEditor;
import com.vertexnet.realestate.view.utility.CalenderCellRenderer;
import com.vertexnet.realestatenet.model.bean.Customer;
import com.vertexnet.realestatenet.model.bean.Site;

/**
 *
 * @author Srivathsa
 */
public class SitePaymentDialog extends DefaultDialog implements View {

	private static final long serialVersionUID = 1L;
	Map<Site, SitePaymentTableModel> tableMap;
	private Customer customer;
	private Controller controller;
	List<PaymentPlanUI> utilList;

    /**
     * Creates new form SitePurchasedDialog
     */
    public SitePaymentDialog(Controller controller,Customer customer) {
        initComponents();
        this.customer = customer;
        this.controller = controller;
        utilList = new ArrayList<PaymentUtility.PaymentPlanUI>();
        setupDialog();
        
        HibernateUtil.getSession().evict(this.customer);
	}
    
	@Override
	public void setupDialog() {
		tableMap = new HashMap<Site, SitePaymentTableModel>();
		for(Site sitePurchased : customer.getSites()) {
			sitesComboBox.addItem(sitePurchased);
			PaymentUtility utility = new PaymentUtility(sitePurchased);
			List<PaymentPlanUI> planUI = utility.getPlanUI();;
			utilList.addAll(planUI);
			SitePaymentTableModel model = new SitePaymentTableModel(planUI, sitePurchased);
			tableMap.put(sitePurchased, model);
		}
		paymentsPanel.setModel(tableMap.get(customer.getSites().toArray()[0]));
		paymentDetailsLabel.setText("Payment Details for Site " + customer.getSites().toArray()[0]);
		
		paymentsPanel.getColumnModel().getColumn(4).setCellEditor(new CalenderCellEditor());
		paymentsPanel.getColumnModel().getColumn(4).setCellRenderer(new CalenderCellRenderer());
		paymentsPanel.getColumnModel().getColumn(3).setCellEditor(new AgentsComboBoxesCellEditor());
		paymentsPanel.getColumnModel().getColumn(3).setCellRenderer(new AgentsComboBoxCellRenderer());
	}

	/**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        paymentDetailsLabel = new javax.swing.JLabel();
        siteLabel = new javax.swing.JLabel();
        sitesComboBox = new javax.swing.JComboBox();
        jScrollPane2 = new javax.swing.JScrollPane();
        paymentsPanel = new javax.swing.JTable();
        cancelButton = new javax.swing.JButton();
        saveButton = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        paymentDetailsLabel.setText("jLabel1");

        siteLabel.setText("Site : ");

        sitesComboBox.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                sitesComboBoxActionPerformed(evt);
            }
        });

        jScrollPane2.setViewportView(paymentsPanel);

        cancelButton.setText("Cancel");
        cancelButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cancelButtonActionPerformed(evt);
            }
        });

        saveButton.setText("Save");
        saveButton.setName(CUSTOMER.getSave());
        saveButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                saveButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 846, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(paymentDetailsLabel)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(siteLabel)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(sitesComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, 132, javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addGap(0, 0, Short.MAX_VALUE))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGap(0, 0, Short.MAX_VALUE)
                        .addComponent(saveButton)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(cancelButton)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(siteLabel)
                    .addComponent(sitesComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(11, 11, 11)
                .addComponent(paymentDetailsLabel)
                .addGap(4, 4, 4)
                .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 459, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(cancelButton)
                    .addComponent(saveButton))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

	private void sitesComboBoxActionPerformed(java.awt.event.ActionEvent evt) {// GEN-FIRST:event_sitesComboBoxActionPerformed
		if (tableMap.get(sitesComboBox.getSelectedItem()) != null) {
			paymentsPanel.setModel(tableMap.get(sitesComboBox.getSelectedItem()));
			paymentDetailsLabel.setText("Payment Details for Site " + sitesComboBox.getSelectedItem());
			paymentsPanel.getColumnModel().getColumn(4).setCellEditor(new CalenderCellEditor());
			paymentsPanel.getColumnModel().getColumn(4).setCellRenderer(new CalenderCellRenderer());
			paymentsPanel.getColumnModel().getColumn(3).setCellEditor(new AgentsComboBoxesCellEditor());
			paymentsPanel.getColumnModel().getColumn(3).setCellRenderer(new AgentsComboBoxCellRenderer());
		}
	}// GEN-LAST:event_sitesComboBoxActionPerformed

    private void saveButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_saveButtonActionPerformed
    	paymentsPanel.changeSelection(-1, -1, false, false);
    	controller.handleEvent(evt);
    }//GEN-LAST:event_saveButtonActionPerformed

    private void cancelButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancelButtonActionPerformed
		RealEstateUtilityModel.customers.remove(this.customer);
		if (this.customer.getCustomerId() != null) {
			this.customer = (Customer) HibernateUtil.getSession().get(Customer.class, this.customer.getCustomerId());
			RealEstateUtilityModel.customers.add(this.customer);
		}
		this.setVisible(false);
		this.dispose();
    }//GEN-LAST:event_cancelButtonActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton cancelButton;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JLabel paymentDetailsLabel;
    private javax.swing.JTable paymentsPanel;
    private javax.swing.JButton saveButton;
    private javax.swing.JLabel siteLabel;
    private javax.swing.JComboBox sitesComboBox;
    // End of variables declaration//GEN-END:variables
    
    public List<PaymentPlanUI> getPaymentUI() {
    	return utilList;
    }

	public void setRealEstateFrame(MainView view) {

	}

	public void setController(Controller controller) {

	}

	public void showSaveConfirmation() {
		JOptionPane.showMessageDialog(this, "Payment Saved Successfully");
	}

	public void showPaymentIssueDialog(String newValue) {
		JOptionPane.showMessageDialog(this, newValue);
	}

	public Customer getCustomer() {
		return customer;
	}
}
